/*
图表：数据库关系图
createtime: 2024/5/2
*/
(function ($) {
    $.fn.yqChartDbRel = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqChartDbRel.methods[opts](this, param);
        }

        var $this = this;
        var opt;
        if ($this.data("yqtype") == "yqChartDbRel") {
            destroy($this);
        }

        opt = $.extend({}, $.fn.yqChartDbRel.defaults, opts);
        var $uniqueID = $this.attr("ID");
        if ($uniqueID === undefined || $uniqueID === null || $uniqueID == "") {
            $uniqueID = $.getUniqueID();
            $this.attr("ID", $uniqueID);
        }

        if (opt.width === undefined && $this.width() > 0) {
            opt.width = $this.width();
        }
        else if (typeof opt.width != "undefined") {
            $this.width(opt.width);
        }

        if (opt.height === undefined && $this.height() > 0) {
            opt.height = $this.height();
        }
        else if (typeof opt.height != "undefined") {
            $this.height(opt.height);
        }

        opt._selectstate = {};

        //变量信息
        $this.data("chartdbrel_opt", opt);
        $this.data("yqtype", "yqChartDbRel");

        LoadJsCss(function () {
            opt.beforeBindingTitle.call($this, function () {
                init($this);
            });
        });
    };

    $.fn.yqChartDbRel.defaults = {
        value: {},
        isEdit: true,
        menus: [],
        beforeBindingTitle: function (callback) {
            callback();
        }, //标题绑定前
        afterBindingTitle: function () { },   //标题绑定后
        selectTable: function () {   //选择表

        },
        onSave: function (val) {  //保存

        }
    };

    $.fn.yqChartDbRel.methods = {
        options: function (jq) {
            return options(jq);
        },
        resize: function (jq, res) {
            var opt = options(jq);
            resize(jq, res);
        },
        isEdit: function (jq, tf) {
            isEdit(jq, tf);
        },
        destroy: function (jq) {
            var $object = jq;
            destroy($object);
        },
        setValue: function (jq, val) {
            setValue(jq, val);
        },
        clear: function (jq) {
            var opt = options(jq);
            opt.diagram.clear();
        },
        getValue: function (jq) {
            return getValue(jq);
        },
        addNode: function (jq, nodedata) {
            addNode(jq, nodedata);
        },
    }

    function LoadJsCss(callback) {
        var file = [];
        file.push({ url: "/scripts/gojs/go.js", type: "js" });
        $.yqLoadJsCss(file, function () {
            callback();
        })
    }

    function options($object) {
        return $object.data("chartdbrel_opt");
    }

    function destroy($object) {
        var opt = options($object);

        $object.empty();

        $object.removeData("chartdbrel_opt");
        $object.removeData("yqtype");
    }

    function resize($object, res) {
        var opt = options($object);
        var $uniqueID = $object.attr("ID");

        if (res === undefined) {
            res = {};
        }

        if (!(res.width === undefined)) {
            opt.width = res.width;
        }
        if (!(res.height === undefined)) {
            opt.height = res.height;
        }

        var left_w = opt.width - 252;
        if (left_w < 300) {
            left_w = 300;
        }

        $object.css({
            "width": left_w + "px",
            "height": opt.height + "px"
        });

        opt.$diagram_div.css({
            "background-color": "white",
            "width": left_w + "px",
            "height": opt.height + "px"
        });
        opt.$diagram_div.find("canvas").css({
            "width": left_w + "px",
            "height": opt.height + "px"
        });
        opt.$diagram_div.find("div").css({
            "width": left_w + "px",
            "height": opt.height + "px"
        });

        opt.$diagram_edit.yqUiMaskEdit("resize", {
            "width": "250px",
            "height": (opt.height - 37) + "px"
        });
    }

    function isEdit($object, tf) {
        var opt = options($object);
        if (tf === undefined) {
            tf = opt.isEdit;
        } else {
            opt.isEdit = tf;
        }

        opt.diagram.clearSelection();
        opt.diagram.grid.visible = tf;
        opt.diagram.isReadOnly = !tf;

        // 启用用户交互，包括选择、拖拽等  
        opt.diagram.allowSelect = tf;

        var nodes = opt.diagram.nodes;
        var it_node = nodes.iterator;
        while (it_node.next()) {
            var node = it_node.value;
            node.contextMenu = menu_ContextMenu($object); //菜单
        }

        var links = opt.diagram.links;
        var it = links.iterator;
        while (it.next()) {
            var link = it.value;
            link.contextMenu = menu_ContextMenu($object); //菜单
        }
    }

    function form_load($object) {
        var opt = options($object);
        resize($object);
        setValue($object);

        opt.afterBindingTitle.call($object);
    }

    function init($object) {
        var opt = options($object);
        var $uniqueID = $object.attr("ID");

        opt.$ = go.GraphObject.make;

        var $tb = $('<table style="border-spacing: unset;margin: 0;padding: 0;background-color:#f3f3f3;"><tr><td style="margin: 0;padding: 0;"></td style="margin: 0;padding: 0;"><td></td></tr></table>');
        $object.append($tb);
        var $tds = $tb.find("td");

        var diagram_div = $uniqueID + "_Div";
        var html_ = '<div id="' + diagram_div + '" style="overflow: ;flex-grow: 1; border: none;"></div>';
        opt.$diagram_div = $(html_);
        $($tds[0]).append(opt.$diagram_div);

        var diagram_menu = $uniqueID + "_menu";
        var html_ = '<div id="' + diagram_menu + '"></div>';
        opt.$diagram_menu = $(html_);
        $($tds[1]).append(opt.$diagram_menu);

        var menus = [{
            id: "database",
            text: "添加表",
            iconCls: "fa fa-database",
            onClick: function () {
                var prevent_e = $.getPreventDefault(this);
                opt.selectTable.call($object, prevent_e);
            }
        }, {
            id: "del",
            text: "删除",
            iconCls: "icon2-del",
            onClick: function () {
                //console.log(opt._flowstate);
                if (opt._selectstate.node === undefined) {
                    return;
                }
                opt.diagram.commandHandler.deleteSelection();

                opt._selectstate={};
                info_edit($object);
            }
        }, {
            id: "clear",
            text: "清空",
            iconCls: "icon2-clear",
            onClick: function () {
                setValue($object, {});
            }
        }, {},
        {
            id: "save",
            text: "保存",
            iconCls: "icon2-save",
            onClick: function () {
                var val = getValue($object);
                opt.onSave.call(this, val);
            }
        }, {
            id: "info",
            text: "info",
            iconCls: "fa fa-info-circle",
            onClick: function () {
                var prevent_e = $.getPreventDefault(this);
                prevent_e.id = $object.attr("id");
                prevent_e.options = {};

                var w = opt.width - 10;
                var h = opt.height - 43;
                if (w > 500) {
                    w = 500;
                }
                // if (h > 800) {
                //     h = 800;
                // }

                var val = getValue($object);
                var show_pops = {
                    prevent_e: prevent_e,
                    mdiinfo: $.yqGetWindParams(),
                    width: w,
                    height: h,
                    types: ["json"],
                    //types: ["html"],
                    type: "json",
                    value: JSON.stringify(val, null, 2),
                    isEdit: false,
                    goDocEnd: false,  //加载后，显示在尾部
                    callback: function (value) {
                        //b_value = value;
                    }
                };
                if (window.top == window.self) {
                    $.yqCodePopEdit(show_pops);
                }
                else {
                    window.top.$.yqCodePopEdit(show_pops);
                }
            }
        }];

        if (opt.menus.length > 0) {
            menus.push({});
            for (var i = 0; i < opt.menus.length; i++) {
                menus.push(opt.menus[i]);
            }
        }

        opt.$diagram_menu.yqTopMenu({
            menus: menus,
            mini: true,
            afterBindingTitle: function () {

            },
        });


        var diagram_edit = $uniqueID + "_edit";
        var html_ = '<div id="' + diagram_edit + '" style="overflow: ;flex-grow: 1; border: none;"></div>';
        opt.$diagram_edit = $(html_);
        $($tds[1]).append(opt.$diagram_edit);

        opt.colors = [
            { no: 1, name: 'red', value: '#ff3333' },
            { no: 2, name: 'blue', value: '#3358ff' },
            { no: 3, name: 'green', value: '#25ad23' },
            { no: 4, name: 'magenta', value: '#d533ff' },
            { no: 5, name: 'purple', value: '#7d33ff' },
            { no: 6, name: 'orange', value: '#ff6233' },
            { no: 7, name: 'brown', value: '#8e571e' },
            { no: 8, name: 'white', value: '#ffffff' },
            { no: 9, name: 'black', value: '#000000' },
            { no: 10, name: 'beige', value: '#fffcd5' },
            { no: 11, name: 'extralightblue', value: '#d5ebff' },
            { no: 12, name: 'extralightred', value: '#f2dfe0' },
            { no: 13, name: 'lightblue', value: '#a5d2fa' },
            { no: 14, name: 'lightgray', value: '#cccccc' },
            { no: 15, name: 'lightgreen', value: '#b3e6b3' },
            { no: 16, name: 'lightred', value: '#fcbbbd' }
        ];

        opt.field_type = [
            { no: 1, "name": "维度", pic: "" },
            { no: 2, "name": "度量", pic: "" },
            { no: 3, "name": "时间维度", pic: "" },

            { no: 6, "name": "维度主键", pic: "" },
            { no: 7, "name": "维度描述", pic: "" },
            { no: 9, "name": "其它", pic: "" }
        ];

        opt.diagram = new go.Diagram(diagram_div, {
            //"contentAlignment": go.Spot.TopLeft,
            "grid": opt.$(go.Panel,
                'Grid',
                opt.$(go.Shape, 'LineH', { stroke: 'lightgray', strokeWidth: 0.5 }),
                opt.$(go.Shape, 'LineV', { stroke: 'lightgray', strokeWidth: 0.5 })
            ),
            "validCycle": go.CycleMode.NotDirected,
            "undoManager.isEnabled": true,
            "click": function (e) {
                info_edit($object);
            },
        });

        var fieldTemplate = opt.$(go.Panel,
            'TableRow',
            {
                background: 'transparent',
                fromSpot: go.Spot.LeftRightSides,
                toSpot: go.Spot.LeftRightSides,
                //fromLinkable: true, 
                //toLinkable: true
            },
            new go.Binding('portId', 'name'),
            new go.Binding('fromLinkable', '', function (d, e) {
                //return true;
                return e.part.data.isdata;
            }),
            new go.Binding('toLinkable', '', function (d, e) {
                return true;
                //!(e.part.data.isdata);
            }),
            opt.$(go.Picture,
                {
                    source: "data:image/png;base64,",
                    desiredSize: new go.Size(20, 20),
                    //alignment: go.Spot.TopRight,
                    //margin: new go.Margin(0, 0, 0, 0),
                },
                new go.Binding('source', '', function (d, e) {
                    var val = opt.field_type[opt.field_type.length - 1].pic;
                    if (d.type === undefined) {
                        return val;
                    }

                    var v = d.type;
                    //var val = '#3358ff';
                    for (var i = 0; i < opt.field_type.length; i++) {
                        if (opt.field_type[i].no == v) {
                            val = opt.field_type[i].pic;
                            break;
                        }
                    }
                    return val;
                })
            ),

            opt.$(go.TextBlock,
                {
                    margin: new go.Margin(3, 5),
                    column: 1,
                    font: 'bold 13px sans-serif',
                    alignment: go.Spot.Left,
                    fromLinkable: false,
                    toLinkable: false,
                },
                new go.Binding('text', 'name')
            ),
            opt.$(go.TextBlock,
                {
                    margin: new go.Margin(0, 5),
                    column: 2,
                    font: '13px sans-serif',
                    alignment: go.Spot.Left,
                },
                new go.Binding('text', 'info')),
        );

        opt.diagram.nodeTemplateMap.add("", opt.$(go.Node,
            'Auto',
            {
                copyable: false,
                deletable: true,
                selectionChanged: function nodeSelectionChanged(node) {
                    if (!(node.data.model === undefined)) {
                        return;
                    }

                    var s_type = 1;
                    if (node.isSelected) {
                        opt._selectstate = {
                            node: node,
                            type: s_type
                        };
                        var nodeData = node.data;
                        opt.$diagram_edit.yqUiMaskEdit("setValue", { ui_type: s_type, value: nodeData });
                    } else {
                        opt._selectstate = {
                        };
                    }
                }
            },
            new go.Binding('location', 'loc', go.Point.parse).makeTwoWay(go.Point.stringify),
            opt.$(go.Shape, { fill: '#EEEEEE' }),

            opt.$(go.Panel,
                'Vertical',
                opt.$(go.Panel,
                    'Auto',
                    {
                        stretch: go.Stretch.Horizontal
                    },
                    opt.$(go.Shape, { fill: '#1570A6', stroke: null },
                        new go.Binding('fill', 'isdata', function (isdata) {
                            return isdata ? "#1570A6" : "#58c9ff";
                        })
                        // "#F7B84B"
                    ),

                    opt.$(go.TextBlock,
                        {
                            alignment: go.Spot.Center,
                            margin: 3,
                            stroke: 'white',
                            textAlign: 'center',
                            font: 'bold 12pt sans-serif',
                        },
                        new go.Binding('text', 'name')
                    )
                ),

                opt.$(go.Panel,
                    'Table',
                    {
                        padding: 2,
                        minSize: new go.Size(100, 10),
                        defaultStretch: go.Stretch.Horizontal,
                        itemTemplate: fieldTemplate,
                    },
                    new go.Binding('itemArray', 'fields')
                )
            )
        ));

        opt.diagram.linkTemplateMap.add("", opt.$(go.Link,
            {
                relinkableFrom: true,
                relinkableTo: true,
                toShortLength: 4,
                fromShortLength: 2,
                toPortChanged: function (thisLink, oldPort, newPort) { //添加连线属性
                    var data = thisLink.data;
                    if (data.join === undefined) {
                        data.join = 1;
                    }
                },
                selectionChanged: function lineSelectionChanged(line) {
                    var nodeData = line.data;
                    var s_type = 2;
                    if (line.isSelected) {
                        opt._selectstate = {
                            node: line,
                            type: s_type
                        };

                        opt.$diagram_edit.yqUiMaskEdit("setValue", { ui_type: s_type, value: nodeData });
                    } else {
                        opt._selectstate = {
                        };
                    }
                },
            },
            opt.$(go.Shape, { strokeWidth: 1.5 }),
            opt.$(go.Shape, {
                fromArrow: "Circle",
                scale: 0.6
            }),
            opt.$(go.Shape, {
                toArrow: 'Standard',
                scale: 1
            }),
            opt.$(go.TextBlock, // the "from" label
                {
                    text: "1",
                    textAlign: 'center',
                    font: 'bold 14px sans-serif',
                    stroke: 'black',
                    segmentIndex: 0,
                    segmentOffset: new go.Point(NaN, NaN),
                    segmentOrientation: go.Orientation.Upright,
                },
                new go.Binding('text', 'join', function (d) {
                    if (d == 1) {
                        return "n";
                    } else if (d == 2) {
                        return "1";
                    } else if (d == 3) {
                        return "n";
                    } else {
                        return "n";
                    }
                })
            ),
            opt.$(go.TextBlock, // the "to" label
                {
                    text: "1",
                    textAlign: 'center',
                    font: 'bold 14px sans-serif',
                    stroke: 'black',
                    segmentIndex: -1,
                    segmentOffset: new go.Point(NaN, NaN),
                    segmentOrientation: go.Orientation.Upright,
                },
                new go.Binding('text', 'join', function (d) {
                    if (d == 1) {
                        return "1";
                    } else if (d == 2) {
                        return "n";
                    } else if (d == 3) {
                        return "n";
                    } else {
                        return "1";
                    }
                })
            )
        ));

        init_itemedit($object);
    }

    function menu_ContextMenu($object) {
        var opt = options($object);

        if (opt.isEdit) {
            return opt.$('ContextMenu',
                makeLinkButton($object, "删除", function (e, obj) {
                    console.log("2222222222222", e.diagram);
                    e.diagram.commandHandler.deleteSelection()
                }),
            );
        } else {
            return null;
        }

        // var menu = null;
        // if (opt.isEdit) {
        //   var menu =
        //     opt.$("ContextMenu",
        //       makeLinkButton($object, "添加菜单",
        //         function (e, obj) {
        //           addMenuNode($object, e, obj);
        //         }),
        //       makeLinkButton($object, "添加文本",
        //         function (e, obj) {
        //           addLabelNode($object, e, obj);
        //         }),
        //       makeLinkButtonLine($object),
        //       makeLinkButton($object, "删除",
        //         function (e, obj) { e.diagram.commandHandler.deleteSelection() }),
        //       makeLinkButton($object, "编辑",
        //         function (e, node) {
        //           editNode($object, e, node);
        //         }),
        //     );
        // }

        // return menu;
    }

    function makeLinkButton($object, text, action) {
        var opt = options($object);
        return opt.$("ContextMenuButton",
            opt.$(go.Panel, 'Horizontal', {
                //alignment: go.Spot.Center,
            },
                opt.$(go.TextBlock, text),
                {
                    height: 23,
                    width: 100,
                    //stroke: 'lightgray',
                    //fill: color,
                    alignment: go.Spot.Center,
                    margin: 2,
                    background: 'transparent',
                    mouseEnter: function (e, shape) { return (shape.stroke = 'dodgerblue'); },
                    mouseLeave: function (e, shape) { return (shape.stroke = 'lightgray'); },
                    click: action,
                    contextClick: action,
                }));
    }

    function makeLinkButtonLine($object) {
        var opt = options($object);
        return opt.$(go.Shape, "Rectangle", {
            height: 1,
            stretch: go.GraphObject.Horizontal,
            stroke: "#C0C0C0",
            fill: "#C0C0C0"
        });
    }

    function init_itemedit($object) {
        var opt = options($object);

        opt.$diagram_edit.yqUiMaskEdit({
            ui_type: 2,  //ui类型
            value: {},
            isEdit: true,
            treeEdit: false,
            //自定义数据格式接口
            columns: [
                {
                    field: 'name', title: '名称', width: '40%'
                }, {
                    field: 'value',
                    title: '值',
                    width: '45%',
                }
            ],
            afterBindingTitle: function () {
                form_load($object);
            },
            uiMaskLoadInit: function (ui_type, value, data) {
                return itemedit_loadInit($object, ui_type, value);
            },
            endEdit: function (index, row) {
                //console.log("11");
                var s_type = opt.$diagram_edit.yqUiMaskEdit("getUiType");
                if (s_type === 0) {
                    opt.value.info[row.field] = row.value;
                } else {
                    var node = opt._selectstate.node;
                    opt.diagram.model.setDataProperty(node.data, row.field, row.value);
                    if (row.field == "roleType") {
                        var s_type = opt.$diagram_edit.yqUiMaskEdit("getUiType");
                        var nodeData = opt.$diagram_edit.yqUiMaskEdit("getValue");
                        opt.$diagram_edit.yqUiMaskEdit("setValue", { ui_type: s_type, value: nodeData });
                        //console.log(s_type, nodeData);
                    }
                }
            },
        });
    }

    function itemedit_loadInit($object, ui_type, value) {
        var opt = options($object);
        //console.log(ui_type, value);

        var data;
        if (ui_type == 0) {  //节点
            data = {
                "rows": [
                    $.yqDgEditor({
                        field: "code",
                        name: "编码",
                        value: "",
                        group: "基础信息"
                    }, 2, {
                        readonly: false
                    }),
                    $.yqDgEditor({
                        field: "describe",
                        name: "描述",
                        value: "",
                        group: "基础信息"
                    }, 2, {
                        readonly: false
                    }),
                    $.yqDgEditor({
                        field: "remark",
                        name: "备注",
                        value: "",
                        group: "基础信息"
                    }, 5, {
                        readonly: false
                    }),
                ]
            };
        }
        else if (ui_type == 1) {  //节点
            data = {
                "rows": [
                    $.yqDgEditor({
                        field: "name",
                        name: "表名",
                        value: "",
                        group: "基础信息"
                    }, 2, {
                        readonly: false
                    }),

                    $.yqDgEditor({
                        field: "isdata",
                        name: "数据集",
                        value: null,
                        group: "基础信息"
                    }, 3, {
                        readonly: false,
                        default: true
                    }),

                    $.yqDgEditor({
                        field: "parent",
                        name: "父项字段",
                        value: null,
                        group: "基础信息"
                    }, 2, {
                        readonly: false,
                        default: true
                    }),

                    $.yqDgEditor({
                        field: "fields",
                        name: "字段",
                        value: null,
                        group: "基础信息"
                    }, 15, {
                        readonly: false,
                        "param": {
                            "titles": [
                                {
                                    "is_func": false,
                                    "name": "name",
                                    "ui_title": "字段名",
                                    "ui_type": 2,
                                    "ui_width": 100,
                                    "ui_visible": true,
                                    "ui_readonly": true,
                                    "ui_mask": {
                                    }
                                },
                                {
                                    "is_func": false,
                                    "name": "info",
                                    "ui_title": "描述",
                                    "ui_type": 2,
                                    "ui_width": 120,
                                    "ui_visible": true,
                                    "ui_readonly": false,
                                    "ui_mask": {
                                        "default": null,
                                        "editModeW": 300,
                                        "editModeH": 200
                                    }
                                },
                                {
                                    "is_func": false,
                                    "name": "type",
                                    "ui_title": "类型",
                                    "ui_type": 6,
                                    "ui_width": 100,
                                    "ui_visible": true,
                                    "ui_readonly": false,
                                    "ui_mask": {
                                        "default": 1,
                                        "blank": false,
                                        "lists": opt.field_type,
                                        "editModeW": 400,
                                        "editModeH": 300
                                    }
                                }
                            ],
                            "model": []
                        },
                        isAdd: false,
                        isDel: false,
                        "editModeW": 380,
                        "editModeH": 400,
                    })
                ]
            };
        } else if (ui_type == 2) {  //线
            data = {
                "rows": [
                    $.yqDgEditor({
                        field: "join",
                        name: "连接类型",
                        value: null,
                        group: "基础信息"
                    }, 6, {
                        readonly: false,
                        blank: false,
                        default: 1,
                        lists: [
                            {
                                "no": 1,
                                "name": "left"
                            },
                            {
                                "no": 2,
                                "name": "right"
                            },
                            {
                                "no": 3,
                                "name": "inner"
                            }
                        ]
                    })
                ]
            };
        }


        return data;
    }

    //基础信息编辑
    function info_edit($object) {
        var opt = options($object);
        var data_ = opt.value.info;
        if (data_ === undefined) {
            data_ = {};
            opt.value.info = data_;
        }

        opt.$diagram_edit.yqUiMaskEdit("setValue", { ui_type: 0, value: data_ });
    }

    function addNode($object, nodedata) {
        var opt = options($object);
        if (nodedata === undefined) {
            nodedata = {
                "key": $.getShortID(),
            }
        }
        if (nodedata.key === undefined) {
            nodedata.key = $.getShortID();
        }
        if (nodedata.isdata === undefined) {
            nodedata.isdata = false;
        }
        opt.diagram.model.addNodeData(nodedata);
    }

    function setValue($object, value) {
        var opt = options($object);

        if (!(value === undefined)) {
            if (typeof value == "string") {
                value = JSON.parse(value);
            }
            opt.value = value;
        }

        if (!(typeof opt.value === "object")) {
            opt.value = {
                nodes: [],
                links: []
            };
        }
        if (!Array.isArray(opt.value.nodes)) {
            opt.value.nodes = [];
        }
        if (!Array.isArray(opt.value.links)) {
            opt.value.links = [];
        }
        if (opt.value.info === undefined) {
            opt.value.info = {};
        }

        opt.value.info = opt.value.info;

        opt.diagram.model = new go.GraphLinksModel(
            {
                "class": "GraphLinksModel",
                "copiesArrays": true,
                "copiesArrayObjects": true,
                "linkFromPortIdProperty": "fromPort",
                "linkToPortIdProperty": "toPort",
                "nodeDataArray": opt.value.nodes,
                "linkDataArray": opt.value.links
            }
        );

        opt._selectstate = {};
        info_edit($object);

        isEdit($object);
    }

    function getValue($object) {
        var opt = options($object);
        var value = JSON.parse(opt.diagram.model.toJson());
        var j_ = {
            info: opt.value.info,
            nodes: value.nodeDataArray,
            links: value.linkDataArray
        };

        for (var i = 0; i < j_.nodes.length; i++) {
            var locs = j_.nodes[i].loc.split(" ");
            j_.nodes[i].loc = (locs[0].indexOf(".") > 0 ? parseFloat(locs[0]).toFixed(2) : locs[0])
                + " "
                + (locs[1].indexOf(".") > 0 ? parseFloat(locs[1]).toFixed(2) : locs[1]);
        }

        for (var i = 0; i < j_.links.length; i++) {
            if (j_.links[i].points) {
                var points = [];
                for (var j = 0; j < j_.links[i].points.length; j++) {
                    var point = j_.links[i].points[j].toString();
                    if (point.indexOf(".") > 0) {
                        point = parseFloat(j_.links[i].points[j].toFixed(2));
                    } else {
                        point = j_.links[i].points[j];
                    }
                    points.push(point);
                }
                j_.links[i].points = points;
            }
        }

        return j_;
    }

})(jQuery);
